#!/bin/sh

########################################################################
# This test checks kedr_payload_register() and kedr_payload_unregister().
#
# [NB] The typical use case when these functions are called once and with 
# correct arguments is implicitly checked, for example, in the tests for 
# call interception, etc. We need to check some other scenarios here.
# 
# Usage: 
#   sh test.sh <scenario_number>
#
# <scenario_number>:
#   0 - register + unregister, then again register + unregister (with 
# correct data each time)
#   1 - attempt to register the same payload twice (the second call must 
# fail)
#   2 - register + attempt to unregister some other payload + unregister
# correctly (must not crash, no explicit checks)
#
# Loading of the payload module must succeed in each scenario. The result of 
# the test is output via "test_passed" parameter (see sysfs).
########################################################################
########################################################################
# A function to check prerequisites: whether the necessary files exist,
# etc.
########################################################################
checkPrereqs()
{
    #if test ! -f "${KEDR_MODULE}"; then
    #    printf "KEDR core module is missing: ${KEDR_MODULE}\n"
    #    exit 1
    #fi
    
    if test ! -f "${PAYLOAD_MODULE}"; then
        printf "Payload module is missing: ${PAYLOAD_MODULE}\n"
        exit 1
    fi
}

########################################################################
# Cleanup function
########################################################################
cleanupAll()
{
    cd "${WORK_DIR}"
    
    @LSMOD@ | grep "${PAYLOAD_NAME}" > /dev/null 2>&1
    if test $? -eq 0; then
        @RMMOD@ "${PAYLOAD_NAME}"
    fi
    
    @LSMOD@ | grep "@KEDR_CORE_NAME@" > /dev/null 2>&1
    if test $? -eq 0; then
        @RMMOD@ "@KEDR_CORE_NAME@"
    fi
}

########################################################################
# doTest() - preform the actual testing
########################################################################
doTest()
{
    @KEDR_CORE_LOAD_COMMAND@ || exit 1

    @INSMOD@ "${PAYLOAD_MODULE}" scenario_number=${SCEN_NO}
    if test $? -ne 0; then
        printf "Failed to load the payload module\n"
        cleanupAll
        exit 1
    fi
    
    OUT_PARAM_FILE="/sys/module/${PAYLOAD_NAME}/parameters/test_passed"
    if test ! -e "${OUT_PARAM_FILE}"; then
        printf "Parameter file does not exist: ${OUT_PARAM_FILE}\n"
        cleanupAll
        exit 1
    fi

    # Save the result to be analyzed below
    TEST_PASSED=$(cat "${OUT_PARAM_FILE}")

    @RMMOD@ "${PAYLOAD_NAME}"
    if test $? -ne 0; then
        printf "Failed to unload the payload module: ${PAYLOAD_NAME}\n"
        cleanupAll
        exit 1
    fi

    @RMMOD@ "@KEDR_CORE_NAME@" || exit 1
    
    # Check the saved result
    printf "Test result (1 - passed, other value - failed): ${TEST_PASSED}\n"
    if test "t${TEST_PASSED}" != "t1"; then
        exit 1
    fi
}

########################################################################
# main
########################################################################
WORK_DIR=${PWD}

if test $# -ne 1; then
    printf "Usage: sh test.sh <scenario_number>\n"
    exit 1
fi

SCEN_NO=$1
printf "Scenario number is ${SCEN_NO}\n"

PAYLOAD_NAME="test_reg_payload"
PAYLOAD_MODULE="payload/${PAYLOAD_NAME}.ko"

checkPrereqs

printf "Kedr core module: ${KEDR_MODULE}\n"
printf "Payload module: ${PAYLOAD_MODULE}\n"

doTest

# just in case
cleanupAll

# test passed
exit 0
